$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'configPointSku/configpointsku/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '商品sku,产品id', name: 'productNmae', index: 'sku', width: 80 },
            { label: '商品名', name: 'sku', index: 'sku', width: 80 },
            { label: '兑换商品所需积分', name: 'needPoints', index: 'need_points', width: 80 },
			{ label: '优惠券兑换数量', name: 'exchangeNum', index: 'exchange_num', width: 80 }, 			
			{ label: '间隔天数', name: 'intervalTime', index: 'interval_time', width: 80 }, 			
			{ label: '是否上架:0-下架 1-上架', name: 'marketable', index: 'marketable', width: 80 }, 			
			{ label: '开始时间', name: 'startTime', index: 'start_time', width: 80 }, 			
			{ label: '结束时间', name: 'endTime', index: 'end_time', width: 80 }, 			
			{ label: '备注', name: 'remark', index: 'remark', width: 80 }, 			
			{ label: '排序', name: 'pOrder', index: 'p_order', width: 80 }			
        ],
		viewrecords: true,
        height: 700,
        rowNum: 25,
		rowList : [25,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            // root: "page.list",
            // page: "page.currPage",
            // total: "page.totalPage",
            // records: "page.totalCount"
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    // 线获取添加模板基本信息
    $.get(baseURL + "goodsCategory/goodscategory/getCategoryTree", function(r){
        vm.categoryTree = r.addInfo.categoryTree;
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		configPointSku: {
			sku:null,
			attributes:null,
		},
        goodsCategoryDialogVisible:false,
        categoryId: null, //分类id
        categoryTree:[], //商品分类树
		catId:null,
        spuTableData:null,
        skuTableData:null,
        spuQuery:{
			goodsId:null,
			name:null,
			catId:null
		},
		q:{
			pointName:null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.configPointSku = {
				sku:null,attributes:null,
			};
			vm.categoryId = [];
			vm.spuTableData = null;
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.configPointSku.id == null ? "configPointSku/configpointsku/save" : "configPointSku/configpointsku/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.configPointSku),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "configPointSku/configpointsku/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			vm.spuTableData = null;
			vm.skuTableData = null;
			$.get(baseURL + "configPointSku/configpointsku/info/"+id, function(r){
                vm.configPointSku = r.configPointSku;
            });
		},
        reset: function () {
            vm.q.pointName = '';
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    'pointName': vm.q.pointName,
                }
            }).trigger("reloadGrid");
		},
        choiceCategory:function(){
            vm.goodsCategoryDialogVisible = true;
		},
        //树结构选中变化时
        categoryChange:function(value){
			;
            //取到末级的分类id
            vm.catId = value[value.length-1];
            //调用接下来的步骤
            vm.goodsCategoryDialogVisible = false;
            //加载spu数据
            vm.loadSpuData();
            // vm.spuQuery = vm.catId;
            // vm.spuGrid();
        },
		//**************************--------------------------------------
        loadSpuData:function(){
		    //ajax请求获得该分类下的spu列表
            // $.get(baseURL + "goods/goods/goodsList/"+catId, function(r){
             //    vm.spuTableData = r.spuTableData;
            // });
            var query = {};
            if(!isBlank(vm.spuQuery.goodsId)){
                query.goods_id= vm.spuQuery.goodsId;
            }
            if(!isBlank(vm.spuQuery.name)){
                query.name= vm.spuQuery.name;
            }
            query.disabled = 0;
            query.category_id = vm.catId;
            query.point_goods = 1;
            $.ajax({
                type: "POST",
                url: baseURL + "goods/goods/goodsList",
                contentType: "application/json",
                data: JSON.stringify(query),
                success: function(r){
                    if(r.code == 0){
                        vm.spuTableData = r.spuTableData;
                    }else{
                        alert(r.msg);
                    }
                }
            });
            vm.skuTableData = null
        },
        choiceSpu:function(row){
			;
			var goodsId = row.goodsId
			if(isBlank(goodsId)){
                vm.$message({
                    showClose: true,
                    message: '异常信息:spu不存在,请联系管理员!',
                    type: 'warning'
                });
				return;
			}
			var skuQuery = {}
			skuQuery.disabled = 0;
			skuQuery.goods_id = goodsId;
            $.ajax({
                type: "POST",
                url: baseURL + "goods/goodsproduct/selectProductByGoodsId",
                contentType: "application/json",
                data: JSON.stringify(skuQuery),
                success: function(r){
                    if(r.code == 0){
                    	;
                        vm.skuTableData = r.skuTableData;
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
        choiceSku:function(row){
			//获得skuId和规格属性中文
			;
			vm.configPointSku.sku = row.productId;
            vm.configPointSku.attributes = row.optionNames;
		},
        choiceTime:function(){
			//时间选择判断
			if(vm.configPointSku.endTime < vm.configPointSku.startTime){
                this.$notify({
                    title: '提示',
                    message: '结束时间不得小于开始时间!',
                    type: 'warning',
                    duration:2000,
                    position: 'bottom-left'
                });
                vm.configPointSku.endTime = null;
			}
		}

	}
});